package erQiao.yfl.app.controller;

import erQiao.yfl.app.common.Result;
import erQiao.yfl.app.controller.Vo.CircleVo;
import erQiao.yfl.app.controller.Vo.MyCircleVo;
import erQiao.yfl.app.dao.entity.Circle;
import erQiao.yfl.app.service.CircleService;
import erQiao.yfl.app.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *圈子
 */
@Controller
@RequestMapping("/circle")
public class CircleController {
    @Resource
    private CircleService circleService;
    @Resource
    private CommentService commentService;

    @RequestMapping("/selectCircleById")
    public Result selectCircleById(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("id"));
        return Result.wrapSuccessResult(circleService.selectByPrimaryKey(id));
    }

    @RequestMapping("/delCircleById")
    public void delCircleById(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("id"));
        circleService.delCircleById(id);
    }

    /**
     * 修改
     * @param circleVo
     */
    @RequestMapping("/updateByPrimaryKey")
    public void updateByPrimaryKey(@RequestBody CircleVo circleVo){
        Circle circle = new Circle();
        BeanUtils.copyProperties(circleVo,circle);
        circleService.updateByPrimaryKey(circle);
    }


    /**
     * 新增
     * @param circleVo
     */
    @RequestMapping("/insertCircle")
    public void insertCircle(@RequestBody CircleVo circleVo){
        Circle circle = new Circle();
        BeanUtils.copyProperties(circleVo,circle);
        circleService.insertCircle(circle);
    }


    /**
     * 点赞
     * @param circleVo
     */
    @RequestMapping("/updteUpvote")
    public void commentCircle(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("id"));
        circleService.updteUpvote(id);
    }


    /**
     * 查登陆人的圈子
     * @param request
     * @return
     */
    @RequestMapping("/selectMyCircleByUserId")
    public Result selectMyCircleByUserId(HttpServletRequest request){
        List<MyCircleVo> myCircleVos = new ArrayList<MyCircleVo>();
        int id = Integer.valueOf(request.getParameter("id"));
        List<Circle> circles = circleService.selectMyCircleByUserId(id);
        BeanUtils.copyProperties(myCircleVos,circles);
        if (circles.size()!=0) {
            for(int i = 0;i<myCircleVos.size();i++){
                int count = commentService.selectComentCountByCid(circles.get(i).getId(),1);
                myCircleVos.get(i).setCount(count);
            }
        }
        return Result.wrapSuccessResult(myCircleVos);
    }



}
